import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';

@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss'],
})
export class Tab2Page {
  constructor(public http: HttpClient) {}
  res: Result;
  ngOnInit(): void {
    const url = 'http://101.96.128.94:9999/data/product/list.php?pno=1';
    this.http.get(url).subscribe((res: any) => {
      console.log(res);
      this.res = res;
    });
  }
  doRefresh(e) {
    const url = 'http://101.96.128.94:9999/data/product/list.php?pno=1';
    this.http.get(url).subscribe((res: any) => {
      console.log(res);
      this.res = res;
      //终止刷新动画
      e.target.complete();
    });
  }
  loadData(e) {
    const url =
      'http://101.96.128.94:9999/data/product/list.php?pno=' +
      (this.res.pno + 1);
    this.http.get(url).subscribe((res: any) => {
      console.log(res);
      // 新值旧值合并
      res.data = this.res.data.concat(res.data);
      this.res = res;
      // 本次加载更多操作结束，准备监听下一次
      e.target.complete();
    });
  }
}

interface Result {
  data: ResultData[];
  pageCount: number;
  pageSize: number;
  pno: number;
  recordCount: number;
}
interface ResultData {
  is_onsale: string;
  lid: string;
  pic: string;
  price: string;
  sold_count: string;
  title: string;
}
